#============================================
# #知识点
#服务端tcp
import socketserver
class myRequestHandler(socketserver.BaseRequestHandler):
    def handle(self) -> None:
        obj = self.request  #相当于conn 对象
        print(self.client_address)
        while True:
            try:
                data = obj.recv(1024)
                if len(data) ==0:    
                    break
                print(data.decode("utf-8"))
                obj.send(data.upper())
            except:
                break

s = socketserver.ThreadingTCPServer(("127.0.0.1",8888),myRequestHandler)
s.serve_forever()


#客户端  
import socket
obj = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
obj.connect(("127.0.0.1",8888))
while True:
    msg = input("输入一段话:")
    if len(msg) == 0:
        continue
    obj.send(msg.encode("utf-8"))
    data = obj.recv(1024)
    print(data.decode("utf-8"))
#============================================
#服务端udp
import socketserver
class myRequestHandler(socketserver.BaseRequestHandler):
    def handle(self) -> None:
        data = self.request[0]  #接受数据
        sever = self.request[1]   #接收的对象
        ip_port=self.client_address
        sever.sendto(data.upper(1024),ip_port)

s = socketserver.ThreadingUDPServer(("127.0.0.1",8888),myRequestHandler)
s.serve_forever()

#客户端  
import socket
obj = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
x = input("输入字母")
obj.sendto(x.encode("utf-8"),("127.0.0.1",8888))
data,ip_port = obj.recvfrom(1024)
print(data.decode("utf-8"))
#============================================

